<template>
  <div class="settings-container">
    <el-card>
      <el-row>
        <el-button
          size="samll"
          type="primary"
          class="el-icon-plus"
          @click="showDialog = true"
          >新增角色</el-button
        >
      </el-row>
      <el-tabs>
        <el-tab-pane label="公司管理">
          <el-table :data="list" border="">
            <el-table-column
              label="序号"
              align="center"
              width="120px"
              type="index"
            />
            <el-table-column
              label="名称"
              align="center"
              width="240px"
              prop="name"
            />
            <el-table-column label="描述" align="center" prop="description" />
            <el-table-column label="操作" align="center">
              <template slot-scope="{ row }">
                <el-button type="success" size="small">权限分配</el-button>
                <el-button type="primary" size="small" @click="editRole(row.id)"
                  >编辑</el-button
                >
                <el-button type="waring" size="small" @click="delRole(row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页栏 -->
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <el-pagination
              layout="prev,pager,next"
              :page="params.page"
              :page-size="params.pagesize"
              :total="total"
              @current-change="changePage"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司设置">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            :closable="false"
            show-icon
          />
          <el-form label-width="120px">
            <el-form-item prop="name" label="公司名称">
              <el-input
                v-model="companyInfo.name"
                style="width: 400px"
                disabled
              />
            </el-form-item>
            <el-form-item label="公司地址" prop="companyAddress">
              <el-input
                v-model="companyInfo.companyAddress"
                style="width: 400px"
                disabled
              />
            </el-form-item>
            <el-form-item label="公司邮箱" prop="mailbox">
              <el-input
                v-model="companyInfo.mailbox"
                style="width: 400px"
                disabled
              />
            </el-form-item>
            <el-form-item porp="remarks" label="公司备注">
              <el-input
                v-model="companyInfo.remarks"
                style="width: 400px"
                disabled
                type="textarea"
                :rows="3"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

      <!-- 对话框 -->
      <el-dialog :visible="showDialog" title="添加角色" @close="btnCancel">
        <el-form
          ref="form"
          label-width="120px"
          :model="formData"
          :rules="rules"
        >
          <el-form-item label="角色名称" style="width: 80%" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="角色描述" style="width: 80%" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOk(formData)"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog></el-card
    >
  </div>
</template>
<script>
import {
  getRoleList,
  getCompanyInfo,
  delRole,
  getRoleDetail,
  updateRole,
  addRole
} from '@/api/settings'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      list: [],
      params: {
        pagesize: 2,
        page: 1
      },
      total: 0,
      showDialog: false,
      companyInfo: {}, // 公司信息
      formData: {}, // 角色信息
      rules: {
        name: [
          {
            required: true,
            message: '名称不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.params)
      this.total = total
      this.list = rows
    },
    changePage(e) {
      this.params.page = e
      this.getRoleList()
    },
    // 编辑角色
    async editRole(id) {
      this.formData = await getRoleDetail(id)
      // console.log(this.formData, '###############')
      this.showDialog = true
    },

    // 获取公司信息
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.companyId)
      // console.log(this.companyInfo, '@@@@@@@@@@@@@@@@@@@@@')
    },

    // 删除角色
    async delRole(id) {
      try {
        await this.$confirm('您确定要删除角色么？')
        await delRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功！！')
      } catch (error) {
        console.log(error)
      }
    },

    // 跟新角色信息
    async btnOk(data) {
      try {
        await this.$refs.form.validate()
        if (this.formData.id) {
          await updateRole(data)
        } else {
          await addRole(data)
        }

        this.getRoleList()
        this.$message.success('操作成功！')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },

    // 取消跟新
    btnCancel() {
      this.formData = {}
      this.$refs.form.resetFields()
      this.showDialog = false
    }
  }
}
</script>
<style scoped></style>
